﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Pout_Porri.Objects.Interface;
using Pout_Porri.Utils;

namespace Pout_Porri.Objects.Bosses
{
    class BigGhost : Enemy, ICollisionBoundaries
    {
        private Animation BigGhostStand, BigGhostRunning, BigGhostPopUp, BigGhostDying;
        private double elapsedTime;
        private float frameRate;
        private int popupcount = 0;
        private int runningcount = 0;
        private int deadcount = 0;



        public BigGhost(Texture2D image, Vector2 position, Vector2 velocity)
            : base(image, position, velocity)
        {
            this.texture = image;
            this.Mass = 0.5f;
            setGrounded(true);
            this.frameRate = 10;
            this.DefaultVelocityX = velocity.X;

        }

        public override void configAnimations()
        {
            BigGhostStand = new Animation("bigghoststand", new Vector2(168, 168), 200, 2, 3, 4, Axis.X);
            BigGhostRunning = new Animation("bigghostrunning", new Vector2(168, 168), 200, 4, 3, 8, Axis.X);
            BigGhostPopUp = new Animation("bigghostpopup", new Vector2(168, 168), 800, 0, 3, 4, Axis.X);
            BigGhostDying = new Animation("bigghostdying", new Vector2(168, 168), 200, 0, 4, 4, Axis.X);

            this.addAnimation(BigGhostStand);

            this.addAnimation(BigGhostRunning);

            this.addAnimation(BigGhostPopUp);

            this.addAnimation(BigGhostDying);

            this.spriteTexture = new Texture2D(Game1.graphics.GraphicsDevice, 168, 168);

            this.setCurrentAnimation("bigghostpopup");
            
            currentState = EnemyStates.popup;
        }

        protected override void applyMovement(GameTime gameTime)
        {
            this.applyMovement();

            //base.applyMovement(gameTime);
        }

        public override void update(GameTime gameTime)
        {
            //checkMatrixCollisionInY();

            if (popupcount <= 150)
            {
                popupcount++;
            }

            if (popupcount == 150)
            {
                currentState = EnemyStates.stand;
            }

            if (currentState == EnemyStates.running)
            {
                runningcount++;
                if (runningcount >= 180)
                {
                    currentState = EnemyStates.stand;
                    runningcount = 0;
                    deadcount = 0;
                }
            }

            if (currentState == EnemyStates.dying)
            {
                deadcount ++;
                if (deadcount >= 150)
                {
                    currentState = EnemyStates.stand;
                    runningcount = 0;
                    deadcount = 0;
                }
            }

            
            // funcao ao ativar o POW que ainda está em teste
            if (Game1.keyboardState.IsKeyDown(Keys.G) && Game1.previusKeyboardState.IsKeyDown(Keys.G))
            {
                if (currentState == EnemyStates.stand)
                {
                    currentState = EnemyStates.running;               
                }
            }

            if (Game1.keyboardState.IsKeyDown(Keys.H) && Game1.previusKeyboardState.IsKeyDown(Keys.H))
            {
                if (currentState == EnemyStates.running)
                {
                    currentState = EnemyStates.dying;
                }
            }



            
            //applyGravity(null);

            if (currentState != EnemyStates.popup)
            {
                IA(MyLevel.getPlayer());
            }

            if (currentState == EnemyStates.running)
            {
                this.setCurrentAnimation("bigghostrunning");

            }
            
            else if (currentState == EnemyStates.stand)
            {
                this.setCurrentAnimation("bigghoststand");
            }

            else if (currentState == EnemyStates.dying)
            {
                this.setCurrentAnimation("bigghostdying");
            }

            Walk(gameTime);

            currentAnimationRect = this.AnimationRect(this.getCurrentAnimation());

            //collisionBoundaries();

            base.update(gameTime);
        }

        public void Walk(GameTime gameTime)
        {
            if (gameTime.TotalGameTime.TotalMilliseconds > elapsedTime + frameRate)
            {
                elapsedTime = gameTime.TotalGameTime.TotalMilliseconds;

                applyMovement(gameTime);
            }


        }

        public void IA(PhysicsGameEntity player)
        {
            Game1.window.Title = "" + this.position.X + this.direction.X;

            if (currentState == EnemyStates.stand)
            {
                if (player.position.X == this.position.X + 21)
                {

                    this.position.X += 0;
                    currentState = EnemyStates.stand;
                }

                else if (this.position.X < player.position.X - 21)
                {

                    this.position.X += 1;
                    //currentState = EnemyStates.walking;
                }

                else if (this.position.X > player.position.X - 21)
                {

                    this.position.X -= 1;
                    //currentState = EnemyStates.walking;
                }

                if (this.position.Y + 42 < player.position.Y && this.position.Y - 42 > player.position.Y)
                {

                    this.position.Y += 0;
                    //currentState = EnemyStates.stand;
                }

                else if (this.position.Y + 42 < player.position.Y)
                {

                    this.position.Y += 1;
                    //currentState = EnemyStates.stand;
                }

                else if (this.position.Y - 42 > player.position.Y)
                {

                    this.position.Y -= 1;
                    //currentState = EnemyStates.stand;
                }
            }

            if (currentState == EnemyStates.running || currentState == EnemyStates.dying)
            {
                if (this.position.X + 42 < player.position.X && this.position.X > 0)
                {

                    this.position.X -= 2;
                    //currentState = EnemyStates.stand;
                }

                else if (this.position.X + 42 > player.position.X && this.position.X < 650)
                {

                    this.position.X += 2;
                    //currentState = EnemyStates.stand;
                }

                //else if (this.position.X < player.position.X&& this.position.X < 600)
                //{

                //    this.position.X += 1;
                //    //currentState = EnemyStates.walking;
                //}

                //else if (this.position.X > player.position.X && this.position.X > 0)
                //{

                //    this.position.X -= 1;
                //    //currentState = EnemyStates.walking;
                //}

                if (this.position.Y + 40 < player.position.Y && this.position.Y > 150)
                {

                    this.position.Y -= 2;
                    //currentState = EnemyStates.walking;
                }

                else if (this.position.Y + 42 > player.position.Y && this.position.Y < 400)
                {

                    this.position.Y += 2;
                    //currentState = EnemyStates.walking;
                }
            }
        }


        private bool isBoundaryLeft;
        public bool IsBoundaryLeft
        {
            get
            {
                return isBoundaryLeft;
            }
            set
            {
                isBoundaryLeft = value;
            }
        }

        public bool IsBoundaryRight
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }


        public void collisionBoundaries()
        {
            if (this.position.X < Game1.graphics.GraphicsDevice.Viewport.Bounds.Left - 20)
            {
                IsBoundaryLeft = true;
                this.position.X = Game1.graphics.GraphicsDevice.Viewport.Bounds.Left - 20;
                this.setVisible(false);
                Game1.window.Title = "" + this.isVisible();
            }
        }



        public bool collisionLadder()
        {
            throw new NotImplementedException();
        }

        public bool collisionHole()
        {
            throw new NotImplementedException();
        }

    }
}
